package com.fadadianqi.search;

import com.fadadianqi.commons.ConfigHelper;
import com.fadadianqi.commons.Constants;
import com.yeqiangwei.commons.io.FileHelper;
import com.yeqiangwei.commons.util.DateTimeUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.File;

public class IndexPathHelper {

    private static final Log log = LogFactory.getLog(IndexPathHelper.class);

    private static final String PREFIX_BUILD_PATH = Constants.ROOT_PATH + "index" + File.separator;

    private static String PROP_INDEX_PATH_KEY = "index.path.";

    private String name; // 索引对象名称用于构造路径

    public static enum IndexEnum {
        CUSTOMER("customer"), GOODS("goods"), SPU("spu"), VILLAGE("village"), SELL("sell"), IN_OUT("inout");

        IndexEnum(String val) {
            this.val = val;
        }

        String val;

        public String getValue() {
            return this.val;
        }
    }

    public IndexPathHelper(IndexEnum flag) {
        this.name = flag.getValue();
    }

    public String createNewPath() {
        String path = PREFIX_BUILD_PATH + name + FileHelper.separator + DateTimeUtils.getTimeInMillis() + File.separator;
        Boolean bea = FileHelper.createFolder(path);
        if (bea) {
            log.info("created new path: " + path);
        } else {
            log.error("Failed to create path (" + path + ")");
        }
        return path;
    }

    public String getIndexPath() {
        String idxPath =  ConfigHelper.getString(PROP_INDEX_PATH_KEY + name);
        log.debug("#getIndexPath# indexPath: " + idxPath);
        return idxPath;
    }

    public void savePath(String path) {
        ConfigHelper.set(PROP_INDEX_PATH_KEY + name, path);
    }

    /**
     * 清理超过10天的索引目录
     */
    public void cleanOldFolder() {

    }
}
